package com.yidu.hbatsams.service.TrainingPlan.zxr;/**
 * @类的描述——周计划Service
 * @author 朱星润
 * @versing 1.0 2019/10/30
 */

import com.github.pagehelper.PageHelper;
import com.yidu.hbatsams.dao.AtsEmpAthleteSchemeMapper;
import com.yidu.hbatsams.dao.AtsPlanCurriculumMapper;
import com.yidu.hbatsams.dao.AtsPlanItemMapper;
import com.yidu.hbatsams.dao.AtsPlanWeekMapper;
import com.yidu.hbatsams.domain.AtsPlanCurriculum;
import com.yidu.hbatsams.domain.AtsPlanWeek;
import com.yidu.hbatsams.domain.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 朱星润
 * @类的描述——
 * @date 2019/10/30下午 04:02
 */
@Service
public class AtsWeekServiceImpl implements AtsWeekService {
    @Resource
    AtsPlanWeekMapper atsWeekMapper;//周计划

    @Resource
    AtsEmpAthleteSchemeMapper atsEmpAthleteSchemeMapper;//运动员计划关联

    @Resource
    AtsPlanItemMapper atsPlanItemMapper;//指标

    @Resource
    AtsPlanCurriculumMapper atsPlanCurriculumMapper;//课计划
    /**
     * 获取当前登录人传进atsWeek对象
     * @param request
     * @param atsWeek
     */
    private void setMangerUser(HttpServletRequest request, AtsPlanWeek atsWeek) {
        UserVo manager = (UserVo) request.getSession().getAttribute("user");
        atsWeek.setType(manager.getCompanyId());
        atsWeek.setFilId(manager.getClerkId());
        atsWeek.setName(manager.getUserName());
    }

    /**
     * 查周计划
     * @param request
     * @param atsWeek
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<AtsPlanWeek> selectAll(HttpServletRequest request, AtsPlanWeek atsWeek, Integer pageNum, Integer pageSize) {
        //获取当前登录人传进atsCurriculum对象
        UserVo manager = (UserVo) request.getSession().getAttribute("user");
        atsWeek.setType(manager.getCompanyId());//角色编码
        atsWeek.setFilId(manager.getClerkId());//角色外键
        atsWeek.setName(manager.getUserName());//角色职业
        PageHelper.startPage(pageNum, pageSize);//分页插件
        List<AtsPlanWeek> list=null;
        if(atsWeek.getType()!=null && !"".equals(atsWeek.getType())){//如果角色编码不为空则进入
            if ("2".equals(atsWeek.getType()) || "5".equals(atsWeek.getType())) {//2，5为教练
                list=atsWeekMapper.findCoach(atsWeek);//查询教练
            } else if ("4".equals(atsWeek.getType())) {//4为运动员
                list=atsWeekMapper.findAthlete(atsWeek);//查运动员
            } else {
                list=atsWeekMapper.findAll(atsWeek);//查所有
            }
        } else {
            list=atsWeekMapper.findAll(atsWeek);//查所有
        }
        return list;
    }

    /**
     * 查周计划总行数
     * @param request
     * @param atsWeek
     * @return
     */
    @Override
    public int count(HttpServletRequest request, AtsPlanWeek atsWeek) {
        //获取当前登录人传进atsCurriculum对象
        UserVo manager = (UserVo) request.getSession().getAttribute("user");
        atsWeek.setType(manager.getCompanyId());//角色编码
        atsWeek.setFilId(manager.getClerkId());//角色外键
        atsWeek.setName(manager.getUserName());//角色职业
        int count=0;
        if(atsWeek.getType()!=null && !"".equals(atsWeek.getType())){//如果角色编码不为空则进入
            if ("2".equals(atsWeek.getType()) || "5".equals(atsWeek.getType())) {//2，5为教练
                count=atsWeekMapper.countCoach(atsWeek);//查询教练
            } else if ("4".equals(atsWeek.getType())) {//4为运动员
                count=atsWeekMapper.countAthlete(atsWeek);//查运动员
            } else {
                count=atsWeekMapper.countWeekAll(atsWeek);//查所有
            }
        } else {
            count=atsWeekMapper.countWeekAll(atsWeek);//查所有
        }
        return count;
    }

    /**
     * 统计图复选框
     * @param request
     * @param atsWeek
     * @return
     */
    @Override
    public List<AtsPlanWeek> comboBox(HttpServletRequest request, AtsPlanWeek atsWeek) {
        //获取当前登录人传进atsCurriculum对象
        UserVo manager = (UserVo) request.getSession().getAttribute("user");
        atsWeek.setType(manager.getCompanyId());//角色编码
        atsWeek.setFilId(manager.getClerkId());//角色外键
        atsWeek.setName(manager.getUserName());//角色职业
        List<AtsPlanWeek> list=null;
        if(atsWeek.getType()!=null && !"".equals(atsWeek.getType())){//如果角色编码不为空则进入
            if ("2".equals(atsWeek.getType()) || "5".equals(atsWeek.getType())) {//2，5为教练
                list=atsWeekMapper.findCoach(atsWeek);//查询教练
            } else if ("4".equals(atsWeek.getType())) {//4为运动员
                list=atsWeekMapper.findAthlete(atsWeek);//查运动员
            } else {
                list=atsWeekMapper.findAll(atsWeek);//查所有
            }
        } else {
            list=atsWeekMapper.findAll(atsWeek);//查所有
        }
        return atsWeekMapper.selectAll(atsWeek);
    }

    /**
     * 主键查询
     * @param weekId
     * @return
     */
    @Override
    public AtsPlanWeek findById(Integer weekId) {
        return atsWeekMapper.findById(weekId);
    }

    @Override
    public List<AtsPlanWeek> weekOrAthleteById(Integer weekId) {
        return atsWeekMapper.weekOrAthleteById(weekId);
    }

    @Override
    public int isvaDel(Integer weekId) {
        //删除对应的运动员
        atsEmpAthleteSchemeMapper.isvaDel(weekId);

        //删除周计划
        int rows = atsWeekMapper.isvaDel(weekId);
        List<AtsPlanCurriculum> list = atsPlanCurriculumMapper.findByWeekId(weekId);//根据课计划外键查询
        for (AtsPlanCurriculum planCurriculum : list) {
            atsPlanItemMapper.isvaDel(planCurriculum.getCurriculumId());//删除指标
        }

        //删除课计划
        atsPlanCurriculumMapper.isvaDel(weekId);
        return rows;
    }
}
